package com.zhonghui.wms.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.zhonghui.common.core.domain.AjaxResult;
import com.zhonghui.common.utils.poi.ExcelUtil;
import com.zhonghui.response.BaseResult;
import com.zhonghui.wms.domain.WmsVehicleSchedulingDetails;
import com.zhonghui.wms.domain.vo.WmsVehicleSchedulingVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhonghui.common.annotation.Log;
import com.zhonghui.common.core.controller.BaseController;
import com.zhonghui.common.enums.BusinessType;
import com.zhonghui.wms.domain.WmsVehicleScheduling;
import com.zhonghui.wms.service.IWmsVehicleSchedulingService;
import com.zhonghui.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 智能仓储WMS-运输管理-车辆调度Controller
 *
 * @author zhonghui
 * @date 2022-05-21
 */
@Api(tags="智能仓储WMS-运输管理-车辆调度")
@RestController
@RequestMapping("/wms/vehicleScheduling")
public class WmsVehicleSchedulingController extends BaseController
{
    @Autowired
    private IWmsVehicleSchedulingService wmsVehicleSchedulingService;

    /**
     * 查询智能仓储WMS-运输管理-车辆调度列表
     */
    @ApiOperation("查询智能仓储WMS-运输管理-车辆调度列表")
    @GetMapping("/list")
    public TableDataInfo<List<WmsVehicleSchedulingVo>> list(WmsVehicleSchedulingVo wmsVehicleSchedulingVo)
    {
        startPage();
        List<WmsVehicleSchedulingVo> list = wmsVehicleSchedulingService.selectWmsVehicleSchedulingList(wmsVehicleSchedulingVo);
        return getDataTable(list);
    }

    /**
     * 导出智能仓储WMS-运输管理-车辆调度列表
     */
    @Log(title = "智能仓储WMS-运输管理-车辆调度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsVehicleSchedulingVo wmsVehicleSchedulingVo)
    {
        List<WmsVehicleSchedulingVo> list = wmsVehicleSchedulingService.selectWmsVehicleSchedulingList(wmsVehicleSchedulingVo);
        ExcelUtil<WmsVehicleSchedulingVo> util = new ExcelUtil<WmsVehicleSchedulingVo>(WmsVehicleSchedulingVo.class);
        util.exportExcel(response, list, "智能仓储WMS-运输管理-车辆调度数据");
    }

    /**
     * 获取智能仓储WMS-运输管理-车辆调度详细信息
     */
    @ApiOperation("获取智能仓储WMS-运输管理-车辆调度详细信息")
    @GetMapping(value = "/{id}")
    public BaseResult<WmsVehicleSchedulingVo> getInfo(@PathVariable("id") Long id)
    {
        return BaseResult.success(wmsVehicleSchedulingService.selectWmsVehicleSchedulingById(id));
    }

    /**
     * 新增智能仓储WMS-运输管理-车辆调度
     */
    @ApiOperation("新增智能仓储WMS-运输管理-车辆调度")
    @Log(title = "智能仓储WMS-运输管理-车辆调度", businessType = BusinessType.INSERT)
    @PostMapping
    public BaseResult<Integer> add(@RequestBody WmsVehicleScheduling wmsVehicleScheduling)
    {
        // 构造查询条件，判断承运单是否已经存在调度列表
        WmsVehicleSchedulingVo schedulingVo = new WmsVehicleSchedulingVo();
        schedulingVo.setCarrierApplicationId(wmsVehicleScheduling.getCarrierApplicationId());
        List<WmsVehicleSchedulingVo> list = wmsVehicleSchedulingService.selectWmsVehicleSchedulingList(schedulingVo);
        if(list != null && !list.isEmpty()) {
            return BaseResult.error("此承运单已经存在车辆调度列表！");
        }
        List<WmsVehicleSchedulingDetails> detailsList = wmsVehicleScheduling.getWmsVehicleSchedulingDetailsList();
        // 判断车辆调度明细表是否为空
        if (detailsList != null && !detailsList.isEmpty()){
            //获取集合中重复的车牌号
            List<String> resultList = detailsList.stream().map(WmsVehicleSchedulingDetails::getCarNumber).distinct().collect(Collectors.toList());
            // 判断集合中是否有重复车牌号，有重复车牌号数量即会和原集合不一致
            if (resultList.size() != detailsList.size()){
                return BaseResult.error("该车辆调度明细中存在重复车牌号！");
            }
        }
        return BaseResult.success(wmsVehicleSchedulingService.insertWmsVehicleScheduling(wmsVehicleScheduling));
    }

    /**
     * 修改智能仓储WMS-运输管理-车辆调度
     */
    @ApiOperation("修改智能仓储WMS-运输管理-车辆调度")
    @Log(title = "智能仓储WMS-运输管理-车辆调度", businessType = BusinessType.UPDATE)
    @PutMapping
    public BaseResult<Integer> edit(@RequestBody WmsVehicleScheduling wmsVehicleScheduling)
    {
        List<WmsVehicleSchedulingDetails> detailsList = wmsVehicleScheduling.getWmsVehicleSchedulingDetailsList();
        // 判断车辆调度明细表是否为空
        if (detailsList != null && !detailsList.isEmpty()){
            //获取集合中重复的车牌号
            List<String> resultList = detailsList.stream().map(WmsVehicleSchedulingDetails::getCarNumber).distinct().collect(Collectors.toList());
            // 判断集合中是否有重复车牌号，有重复车牌号数量即会和原集合不一致
            if (resultList.size() != detailsList.size()){
                return BaseResult.error("该车辆调度明细中存在重复车牌号！");
            }
        }
        return BaseResult.success(wmsVehicleSchedulingService.updateWmsVehicleScheduling(wmsVehicleScheduling));
    }

    /**
     * 删除智能仓储WMS-运输管理-车辆调度
     */
    @ApiOperation("删除智能仓储WMS-运输管理-车辆调度")
    @Log(title = "智能仓储WMS-运输管理-车辆调度", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public BaseResult<Integer> remove(@PathVariable Long[] ids)
    {
        return BaseResult.success(wmsVehicleSchedulingService.deleteWmsVehicleSchedulingByIds(ids));
    }
}
